package com.vcc.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.data.entity.Risk;
import com.vcc.data.mapper.RiskMapper;
import com.vcc.service.boss.response.risk.RiskVoBoss;
import com.vcc.service.service.RiskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.vcc.data.entity.table.BossUserTableDef.BOSS_USER;
import static com.vcc.data.entity.table.RiskRuleTableDef.RISK_RULE;
import static com.vcc.data.entity.table.RiskTableDef.RISK;

@Service
public class RiskServiceImpl extends ServiceImpl<RiskMapper, Risk> implements RiskService {
    @Override
    public Page<RiskVoBoss> paging(Integer pagingNumber, Integer pageSize, Long targetId, String name, Integer status) {
        QueryChain<Risk> queryChain = queryChain()
                .select(RISK.ALL_COLUMNS)
                .select(BOSS_USER.EMAIL.as("operatorAccount"))
                .select(RISK_RULE.ALL_COLUMNS)
                .leftJoin(BOSS_USER).on(BOSS_USER.ID.eq(RISK.OPERATOR_ID))
                .leftJoin(RISK_RULE).on(RISK_RULE.RISK_ID.eq(RISK.ID))
                .where(RISK.NAME.eq(name, StringUtils.isNotBlank(name)))
                .where(RISK.TARGET_ID.eq(targetId, targetId != null))
                .where(RISK.STATUS.eq(status, status != null));
        return queryChain.pageAs(
                Page.of(
                        pagingNumber == null ? 1 : pagingNumber,
                        pageSize == null ? 20 : pageSize
                ),
                RiskVoBoss.class
        );
    }

    @Override
    public Boolean updateStatus(Long id, Integer status) {
        return updateChain()
                .set(RISK.STATUS, status)
                .where(RISK.ID.eq(id))
                .update();
    }

    @Override
    public Boolean updateHasAutoRisk(Long id, Boolean hasAutoRisk) {
        return updateChain()
                .set(RISK.HAS_AUTO, hasAutoRisk)
                .where(RISK.ID.eq(id))
                .update();
    }

    @Override
    public Boolean existsByCode(String code) {
        return queryChain()
                .where(RISK.CODE.eq(code))
                .exists();
    }

    @Override
    public Boolean existsByMccValue(Long userId, String mccValue) {
       return true;
    }

    @Override
    public Boolean existsByCountryValue(Long userId, String countryValue) {
        return true;

    }

    @Override
    public Boolean existsByMerchantValue(Long userId, String merchantValue) {
        return true;

    }

    @Override
    public List<Risk> listByCode(String code, Long cardId, Long userId) {
        return queryChain()
                .where(RISK.CODE.eq(code))
                .where(RISK.STATUS.eq(0))
                .where(RISK.TARGET_ID.eq(0L)
                        .or(RISK.TARGET_ID.eq(cardId, cardId != null))
                        .or(RISK.TARGET_ID.eq(userId, userId != null)))
                .list();
    }

    @Override
    public List<Long> listByTargetId(Long targetId) {
        return queryChain()
                .where(RISK.TARGET_ID.eq(targetId))
                .select(RISK.ID)
                .listAs(Long.class);
    }
}
